package com.xy.sql.xml.kit;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 日期相关计算
 *
 * @author lc liubq
 * @since 20170711
 */
public class XySQLDataUtil {

    /**
     * 转换为列表
     *
     * @param valueObject
     * @return
     */
    public static List<Object> convertList(Object valueObject) {
        List<Object> valueList = new ArrayList<>();
        if (valueObject instanceof Collection) {
            Collection nowList = (Collection) valueObject;
            for (Object v : nowList) {
                valueList.add(v);
            }
        } else if (valueObject.getClass().isArray()) {
            Object[] nowList = (Object[]) valueObject;
            for (Object v : nowList) {
                valueList.add(v);
            }
        } else {
            String[] nowList = valueObject.toString().trim().split(",");
            for (Object v : nowList) {
                valueList.add(v);
            }
        }
        return valueList;
    }

    /**
     * 值转换
     *
     * @param inType
     * @param value
     * @return
     * @throws Exception
     */
    public static Object convertValue(String inType, Object value) {
        if (inType == null) {
            return value;
        }
        if (value == null) {
            return null;
        }
        String type = inType.toUpperCase();
        String tempValue = value.toString().trim();
        if ("DOUBLE".equalsIgnoreCase(type) || "FLOAT".equalsIgnoreCase(type)) {
            if (value instanceof Double || value instanceof Float) {
                return value;
            }
            return Double.valueOf(tempValue);
        } else if ("DECIMAL".equalsIgnoreCase(type)) {
            if (value instanceof BigDecimal) {
                return value;
            }
            return BigDecimal.valueOf(Double.valueOf(tempValue));
        } else if ("TINYINT".equalsIgnoreCase(type) || "SMALLINT".equalsIgnoreCase(type) || "INT".equalsIgnoreCase(type)) {
            if (value instanceof Integer) {
                return value;
            }
            return Integer.valueOf(tempValue);
        } else if ("LONG".equalsIgnoreCase(type) || "BIGINT".equalsIgnoreCase(type)) {
            if (value instanceof Long) {
                return value;
            }
            return Long.valueOf(tempValue);
        } else if ("DATE".equalsIgnoreCase(type) || "DATETIME".equalsIgnoreCase(type) || "TIMESTAMP".equalsIgnoreCase(type)) {
            if (value instanceof Date) {
                return value;
            }
            return XySQLDateUtil.extractToDateTime(tempValue);
        } else {
            return value;
        }
    }

    /**
     * 值转换
     *
     * @param format
     * @param value
     * @return
     * @throws Exception
     */
    public static String formatValue(String format, Object value) {
        if (value == null) {
            return null;
        }
        if (format == null) {
            return value.toString();
        }

        if (value instanceof Date) {
            return XySQLDateUtil.format((Date) value, format);
        } else if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) {
            Double d = Double.valueOf(value.toString());
            int scale = 0;
            int len = format.indexOf(".");
            if (len < 0) {
                scale = 0;
            } else {
                scale = format.length() - (len + 1);
            }
            return BigDecimal.valueOf(d).setScale(scale, RoundingMode.HALF_UP).toString();
        }
        return value.toString();
    }

}
